﻿Namespace Geometry
    Public Class SingleVector
        Private _V_x As Double
        Private _V_y As Double
        Private _V_z As Double
        Private _Azi As Double

        Public Property V_x() As Double
            Get
                Return _V_x
            End Get
            Set(value As Double)
                _V_x = value
            End Set
        End Property

        Public Property V_y() As Double
            Get
                Return _V_y
            End Get
            Set(value As Double)
                _V_y = value
            End Set
        End Property

        Public Property V_z() As Double
            Get
                Return _V_z
            End Get
            Set(value As Double)
                _V_z = value
            End Set
        End Property

        Public Property Azi() As Double
            Get
                Return _Azi
            End Get
            Set(value As Double)
                _Azi = value
            End Set
        End Property

        Public Sub New()
            V_x = 1
            V_y = 0
            V_z = 0
            Azi = Math.PI / 2
        End Sub
        ''' <summary>
        ''' 根据向量值计算方位角，采用函数atan2进行解析
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetAzimuth()
            Dim rad = Math.Atan2(V_y, V_x)
            If rad > Math.PI / 2 Then
                Azi = 5 * Math.PI / 2 - rad
            Else
                Azi = Math.PI / 2 - rad
            End If
            Return Nothing
        End Function
        ''' <summary>
        ''' 根据方位角求向量值，二维（忽略Z）
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetVector()
            Dim tan As Double
            CheckMyself()
            Select Case Azi
                Case 0 To Math.PI / 2
                    tan = Math.Tan(Azi)
                    V_y = 1
                    V_x = tan
                Case Math.PI / 2 To 3 * Math.PI / 2
                    tan = Math.Tan(Azi)
                    V_y = -1
                    V_x = -tan
                Case 3 * Math.PI / 2 To 2 * Math.PI
                    tan = Math.Tan(Azi)
                    V_y = 1
                    V_x = tan
                Case Azi = Math.PI / 2
                    V_x = 1
                    V_y = 0
                Case Azi = 3 * Math.PI / 2
                    V_x = -1
                    V_y = 0
            End Select
            V_z = 0
            Return Nothing
        End Function
        ''' <summary>
        ''' 向量反向
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function Reverse()
            V_x = -1 * V_x
            V_y = -1 * V_y
            V_z = -1 * V_z
            GetAzimuth()
            Return Nothing
        End Function
        ''' <summary>
        ''' 自我检查，将方位角化到0到360度之间
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function CheckMyself()
            If Azi >= 2 * Math.PI Then
                Azi = Azi - 2 * Math.PI
            ElseIf Azi < 0 Then
                Azi = Azi + 2 * Math.PI
            End If
            Return Nothing
        End Function
    End Class
End Namespace
